<template>
  <el-dialog @close="reset" :title="menuData.id>0?'编辑':'新增'" :visible.sync="info.isShow">
    <el-form ref="menuForm" :model="menuData" label-width="80px" :rules="rules">
      <el-form-item label="上级菜单" prop="pid">
        <el-select placeholder="请选择" v-model="menuData.pid">
          <el-option label="顶级菜单" :value="0"> </el-option>
          <el-option
            v-for="item in menuList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单类型" prop="type">
        <el-radio-group v-model="menuData.type">
          <el-radio :label="1" :disabled="menuData.pid>0">目录</el-radio>
          <el-radio :label="2" :disabled="menuData.pid===0">菜单</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单名称" prop="title">
        <el-input placeholder="菜单名称" v-model="menuData.title"></el-input>
      </el-form-item>
      <el-form-item label="菜单图标" v-if="menuData.type === 1" prop="icon">
        <!-- 只有目录有图标 -->
        <el-input placeholder="菜单图标" v-model="menuData.icon"></el-input>
      </el-form-item>
      <el-form-item label="菜单地址" v-if="menuData.type === 2" prop="url">
        <!-- 只有菜单有地址 -->
        <el-input placeholder="菜单地址" v-model="menuData.url"></el-input>
      </el-form-item>
      <el-form-item label="菜单状态" prop="status">
        <el-radio-group v-model="menuData.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  computed: {
    ...mapState("menu", ["menuList"]),
  },
  methods: {
    ...mapActions("menu", ["getMenuListAction"]),
    //   数据重置
    reset() {
      //   关闭对话框组件
      this.info.isShow = false;
      // 重置表单数据
      this.menuData = {
        id:"",
        pid: "",
        title: "",
        type: "",
        status: "",
        icon: "",
        url: "",
      };
    },
    submit() {
      this.$refs.menuForm.validate((valid) => {
        if (valid) {
          //   发送数据请求
          const url=this.menuData.id>0?'/menuedit':'/menuadd'
          this.$axios.post(url, this.menuData).then((res) => {
            if (res.code === 200) {
              // 1-成功提示
              this.$message.success(res.msg);
              // 2-更新菜单列表
              this.getMenuListAction();
              // 3-数据重置
              this.reset();
            }else{
                this.$message.error(res.msg);
            }
          });
        } 
      });
    },
    // 更新和表单元素进行绑定的数据
    setFormData(obj){
      // 更新menuData
      this.menuData={...obj};
    }
  },
  //   props:{
  //       info:Object
  //   },
  props: ["info"],
  data() {
    return {
      //   和表单进行绑定的数据
      menuData: {
        pid: "",
        title: "",
        type: "",
        status: "",
        icon: "",
        url: "",
      },
      //  表单校验规则对象
      rules: {
        pid: { required: true, message: "请选择上级", trigger: "blur" },
        title: { required: true, message: "请输入菜单名称", trigger: "blur" },
        type: { required: true, message: "请选择菜单类型", trigger: "blur" },
        status: { required: true, message: "请选择菜单状态", trigger: "blur" },
      },
    };
  },
};
</script>

<style>
</style>